package com.tianhui.consignortwofei.mvp.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.widget.TabSwitchView;
import com.tianhui.consignortwofei.BuildConfig;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.enty.ProfileInfo;
import com.tianhui.consignortwofei.role.LogisticsRole;
import com.tianhui.consignortwofei.role.ManagerRole;
import com.tianhui.consignortwofei.role.OfficerRole;
import com.tianhui.consignortwofei.role.PlatformRole;
import com.tianhui.consignortwofei.role.RoleController;
import com.tianhui.consignortwofei.role.SalesmanRole;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/4/21
 * @description
 **/
public class SettingActivity extends BaseActivity {

    @BindView(R.id.activity_setting_pushSwitchLinearLayout)
    LinearLayout mPushSwitchLinearLayout;
    @BindView(R.id.activity_setting_pushSwitchImageView)
    ImageView mPushSwitchImageView;
    @BindView(R.id.activity_setting_personalPushSwitchLinearLayout)
    LinearLayout mPersonalPushSwitchLinearLayout;
    @BindView(R.id.activity_setting_personalPushSwitchImageView)
    ImageView mPersonalPushSwitchImageView;
    @BindView(R.id.activity_setting_roleTabSwitchView)
    TabSwitchView mRoleTabSwitchView;

    private boolean mIsPushOpen;
    private boolean mIsPersonalPushOpen;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {

        setTitle("设置");
        initView();
        initListener();
    }

    private void initView() {
        mIsPushOpen = SharedPreferenceManager.getInstance().getPushStatus();
        showPushStatus(mIsPushOpen);
        mIsPersonalPushOpen = SharedPreferenceManager.getInstance().getPersonalPushStatus();
        showPersonalPushStatus(mIsPersonalPushOpen);


        if (BuildConfig.FLAVOR.equals("beta")) {
            int rolePosition = SharedPreferenceManager.getInstance().getRolePosition();
            mRoleTabSwitchView.setDefaultSelection(rolePosition);
            mRoleTabSwitchView.setVisibility(View.VISIBLE);
        } else {
            mRoleTabSwitchView.setVisibility(View.GONE);
        }
    }

    private void showPushStatus(boolean isPushOpen) {
        if (isPushOpen) {
            mPushSwitchImageView.setBackgroundResource(R.mipmap.icon_switch_on);
        } else {
            mPushSwitchImageView.setBackgroundResource(R.mipmap.icon_switch_off);
        }
    }

    private void showPersonalPushStatus(boolean isPushOpen) {
        if (isPushOpen) {
            mPersonalPushSwitchImageView.setBackgroundResource(R.mipmap.icon_switch_on);
        } else {
            mPersonalPushSwitchImageView.setBackgroundResource(R.mipmap.icon_switch_off);
        }
    }

    private void initListener() {
        mPushSwitchLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIsPushOpen = !mIsPushOpen;
                SharedPreferenceManager.getInstance().setPushStatus(mIsPushOpen);
                showPushStatus(mIsPushOpen);
            }
        });

        mPersonalPushSwitchLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIsPersonalPushOpen = !mIsPersonalPushOpen;
                SharedPreferenceManager.getInstance().setPersonalPushStatus(mIsPersonalPushOpen);
                showPersonalPushStatus(mIsPersonalPushOpen);
            }
        });

        mRoleTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                if (position == 0) {
                    RoleController.getInstance().setRole(new ManagerRole());
                } else if (position == 1) {
                    RoleController.getInstance().setRole(new OfficerRole());
                } else if (position == 2) {
                    RoleController.getInstance().setRole(new SalesmanRole());
                } else if (position == 3) {

                    RoleController.getInstance().setRole(new LogisticsRole(null));
                } else if (position == 4) {
                    ProfileInfo profileInfo = new ProfileInfo();
                    profileInfo.roleinfotype = "01";
                    RoleController.getInstance().setRole(new PlatformRole(profileInfo));
                }
                SharedPreferenceManager.getInstance().setRolePosition(position);
                //是否显示统计图View
                EventBusManage.refreshStatisticsView();
                //是否显示承运商管理View
                EventBusManage.refreshCarrierManageView();
            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_security_settings_changePasswordSettingItemView)
    public void changePassword() {
        openActivity(ChangePasswordActivity.class);
    }
}
